package com.vframework.system.org.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vframework.base.exception.BaseException;
import com.vframework.base.model.BaseBean;
import com.vframework.base.service.impl.BaseServiceImpl;
import com.vframework.system.menu.model.MenuBean;
import com.vframework.system.org.mapper.OrgMapper;
import com.vframework.system.org.model.OrgBean;
import com.vframework.system.org.service.OrgService;
import com.vframework.system.role.mapper.RoleMapper;
import com.vframework.system.role.model.RoleBean;
import com.vframework.system.user.model.UserBean;

@Service
public class OrgServiceImpl extends BaseServiceImpl implements OrgService {
	
    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private RoleMapper roleMapper;
    
	public void deleteOrg(String... orgIds) throws BaseException {
	    /*delete org*/
	    orgMapper.deleteOrg(orgIds);
		/*delete user role*/
		orgMapper.deleteUserRole(orgIds);
		/*delete user*/
		orgMapper.deleteOrgUser(orgIds);
		/*delete role menu*/
		orgMapper.deleteRoleMenu(orgIds);
		/*delete role*/
		orgMapper.deleteSysRole(orgIds);
		/*delete org menu*/
		orgMapper.deleteSysOrgMenu(orgIds);
	}
	
	public OrgBean getOrgById(String orgId) throws BaseException {
		return orgMapper.getOrgById(orgId);
	}
	
	public Integer getCount(Map<String, Object> paramMap) throws BaseException {
		return orgMapper.getCount(paramMap);
	}
	
	public List<OrgBean> getOrgList(Map<String, Object> paramMap) throws BaseException {
	    return orgMapper.getOrgList(paramMap);
	}

	public void insertOrg(OrgBean orgBean) throws BaseException {
	    //添加root_path
	    String rootPath = "";
	    if ("0".equals(orgBean.getPorgId())) {
	        rootPath = "0|" + orgBean.getOrgId();
	    } else {
	        //查询父root_path
	        OrgBean porgBean = orgMapper.getOrgById(orgBean.getPorgId());
	        rootPath = porgBean.getRootPath() + "|" + orgBean.getOrgId();
	    }
	    orgBean.setRootPath(rootPath);
	    orgMapper.insertOrg(orgBean);
	}
	
	public Integer getOrgIdExists(String orgId) throws BaseException {
		return orgMapper.getOrgIdExists(orgId);
	}

	public void updateOrg(OrgBean orgBean) throws BaseException {
	    //添加root_path
        String rootPath = "";
        if ("0".equals(orgBean.getPorgId())) {
            rootPath = "0|" + orgBean.getPorgId();
        } else {
            //查询父root_path
            OrgBean porgBean = orgMapper.getOrgById(orgBean.getPorgId());
            rootPath = porgBean.getRootPath() + "|" + orgBean.getOrgId();
        }
	    orgBean.setRootPath(rootPath);
		String oldSiId =  orgBean.getOldOrgId();
		String siId = orgBean.getOrgId();
		//判断原si编号和修改后的是否一致，若一致则直接更新
		if (oldSiId.equals(siId)) {
		    orgMapper.updateOrg(orgBean);
		}else {
			//查询修改后的si编号是否有重复，重复则提示resultMessage，没有重复则更新si信息
			Integer count = orgMapper.getOrgIdExists(orgBean.getOrgId());
			if(count == 0) {
			    orgMapper.updateOrg(orgBean);
			} else {
				throw new BaseException("机构编号重复，请重新修改");
			}
		}
	}

	public List<BaseBean> getMenus(String... ids) throws BaseException {
		return orgMapper.getMenus(ids);
	}

    public void adminAdd(OrgBean orgBean) throws BaseException {
        //添加角色
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("cols", "role_id");
        paramMap.put("tableName", "sys_role");
        String roleId = orgMapper.getMaxId(paramMap);
        RoleBean roleBean = new RoleBean();
        roleBean.setRoleId(roleId);
        roleBean.setRoleName(orgBean.getOrgName() + "机构管理员");
        roleBean.setOrgId(orgBean.getOrgId());
        roleBean.setRoleType("1");//业务角色
        orgMapper.insertOrgRole(roleBean);
        //添加用户
        UserBean userBean = new UserBean();
        userBean.setUserName(orgBean.getOrgName() + "机构管理员");
        userBean.setAccount(orgBean.getAdminAccount());
        userBean.setPassword(orgBean.getAdminPassword());
        userBean.setOrgId(orgBean.getOrgId());
        userBean.setCityCode(orgBean.getCityCode());
        userBean.setStatus("1");
        orgMapper.insertOrgUser(userBean);
        //添加用户权限
        roleBean.setUserId(userBean.getUserId());
        orgMapper.insertUserRole(roleBean);
        //添加管理员菜单
        String ids = orgBean.getOrgId();
        //删除role_menu
        roleMapper.deleteRoleMenu(roleId);
        List<BaseBean> authMenus = orgMapper.getMenus(ids);
        List<MenuBean> menuList = new ArrayList<MenuBean>();
        for (BaseBean baseBean : authMenus) {
            MenuBean menuBean = new MenuBean();
            menuBean.setRoleId(roleId);
            menuBean.setMenuId(baseBean.getCode());
            menuBean.setSelectRole(1);
            menuBean.setAddRole(1);
            menuBean.setUpdateRole(1);
            menuBean.setDeleteRole(1);
            menuBean.setExportRole(1);
            menuBean.setImportRole(1);
            menuList.add(menuBean);
        }
        roleMapper.insertRoleMenu(menuList);
    }

	public void distributeOrgMenu(Map<String, Object> paramMap) throws BaseException {
	    orgMapper.deleteOrgMenu((String)paramMap.get("orgId"));
	    String[] ids = (String[])paramMap.get("ids");
	    if (ids != null && ids.length > 0)
	        orgMapper.insertOrgMenu(paramMap);
	}
}
